"""
Case Type   : 系统视图
Case Name   : 测试新增系统视图，identity_columns字段与数据类型与columns字段与类型对比
Create At   : 2025-11-18
Owner       : lvlintao666
Description :
    1.查看系统视图identity_columns和columns的结构
    2.identity_columns字段与数据类型与columns字段与类型对比是否正确
Expect      :
    1.查看系统视图identity_columns和columns的结构成功
    2.identity_columns字段与数据类型与columns字段与类型对比正确
History     :
"""

import sys
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

logger = Logger()


class IndexIdentityColumns(unittest.TestCase):
    def setUp(self):
        logger.info('----------------this is setup-----------------------')
        logger.info('--------------OpenGauss_Compatibility_D_View_IdentityColumns_Case0002开始执行--------------')
        self.com = Common()
        self.comsh = CommonSH('PrimaryDbDmode')
        self.expect_result_dict = {
            'Column': ['seed_value', 'increment_value', 'last_value', 'is_not_for_replication'],
            'Type': ['sql_variant', 'sql_variant', 'sql_variant', 'bit(1)']}

    def test_Index_Identity_Columns(self):
        logger.info('----------------------------查看表结构-----------------------------')
        msg = self.comsh.execut_db_sql('\d identity_columns')
        logger.info(msg)
        result_dict1 = self.com.format_sql_result(msg)
        del result_dict1['Modifiers']
        logger.info(result_dict1)
        msg = self.comsh.execut_db_sql('\d columns')
        logger.info(msg)
        result_dict2 = self.com.format_sql_result(msg)
        del result_dict2['Modifiers']
        logger.info(result_dict2)
        diff_dict = set(result_dict1['Column']) - set(result_dict2['Column'])
        logger.info(diff_dict)
        logger.info(result_dict1)
        diff_res = self.get_diff_result(result_dict1, diff_dict)
        logger.info(diff_res)
        self.assertDictEqual(self.expect_result_dict, diff_res)

    def get_diff_result(self, source_dict, diff_columns):
        """
        从源字典中筛选差集字段对应的完整数据（Column + Type）
        :param source_dict: 源字典（dict1 或 dict2）
        :param diff_columns: 仅在源字典中存在的 Column 集合
        :return: 差集结果字典（保留原格式）
        """
        result = {'Column': [], 'Type': []}
        # 遍历源字典的 Column 列表，只保留差集字段
        for idx, col in enumerate(source_dict['Column']):
            if col in diff_columns:
                result['Column'].append(col)
                result['Type'].append(source_dict['Type'][idx])
        return result

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        # 无须清理环境
        logger.info(
            '-----------------------OpenGauss_Compatibility_D_View_IdentityColumns_Case0002执行完成-----------------------------')
